<template>
  <e-table :tableData="tableData" :columns="columns" :loading="loading" :pageObj="null">
    <el-table-column slot="action" fixed="right" width="50" align="center">
      <template slot-scope="scope">
        <el-dropdown>
          <a class="el-dropdown-link">
            <i class="el-icon-menu"></i>
          </a>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item @click.native="handleEdit(scope.row)">
              <a>编辑</a>
            </el-dropdown-item>
            <el-dropdown-item @click.native="handleDelete(scope.row)">
              <a class="red">删除</a>
            </el-dropdown-item>
          </el-dropdown-menu>
        </el-dropdown>
      </template>
    </el-table-column>
    <el-table-column slot="name" :label="isDept ? '部门列表' : '区域列表'" align="center">
      <template
        slot-scope="scope"
      >{{ scope.row.flag ? scope.row.flag : (isDept ? scope.row.deptName :scope.row.areaName) }}</template>
    </el-table-column>
  </e-table>
</template>

<script>
// import { queryDeptHead, deleteUserHead } from '@/api/assets'
import { openNotification } from '@/utils/util'

export default {
  data() {
    return {
      loading: false,
      isDept: false,
      // 表头
      columns: [
        {
          label: '负责人',
          prop: 'userName'
        },
        { slot: 'name' },
        { slot: 'action' }
      ],
      tableData: []
    }
  },
  methods: {
    setType(num) {
      console.log('num', num)
      this.isDept = num === 1 ? false : true
    },
    // 编辑回显树
    handleEdit(record) {
      console.log('record', record)
      this.$emit('showTree', record)
    },
    handleDelete(record) {
      const _this = this
      this.$confirm({
        title: '删除操作',
        content: `您确定删除[${record.userName}]这条记录吗？`,
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        centered: true,
        async onOk() {
          // 调用删除接口，刷新表格数据
          console.log('delete', record)
          const index = _this.tableData.findIndex(v => v.userId === record.userId)
          console.log('index', index)
          _this.tableData.splice(index, 1)
          openNotification('success', '删除成功')
        }
      })
    },
    getData() {
      // 父页面从这个页面获取表格数据
      this.$emit('post', this.tableData)
    },
    pushData(obj) {
      const data = { ...obj }
      // 如果已存在，是编辑的情况，则先清空再push
      const index = this.tableData.findIndex(v => v.userId === data.userId)
      if (index > 0) {
        this.tableData.splice(index, 1)
      }
      this.tableData.push(data)
      // console.log('arr', obj, this.tableData)
    }
  }
}
</script>
